package com.fy.module.repo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fy.common.dto.PageDto;
import com.fy.common.exception.ServiceException;
import com.fy.common.vo.ApiRest;
import com.fy.common.vo.PageResult;
import com.fy.module.repo.entity.Repo;
import com.fy.module.repo.mapper.RepoMapper;
import com.fy.module.repo.service.IRepoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 * 题库 服务实现类
 * </p>
 *
 * @author 虚拟宇宙公司
 * @since 2023-03-20
 */
@Service
public class RepoServiceImpl extends ServiceImpl<RepoMapper, Repo> implements IRepoService {

    @Override
    public PageResult<Repo> findPage(PageDto<Repo> pageDto) {
        //数据校验
        if (pageDto == null) {
            throw new ServiceException("参数异常");
        }
        if (pageDto.getNow() <= 0) {
            throw new ServiceException("参数异常");
        }
        if (pageDto.getSize() <= 0) {
            throw new ServiceException("参数异常");
        }
        if (pageDto.getWhere() == null) {
            throw new ServiceException("参数异常");
        }

        //设置分页
        IPage<Repo> page = new Page<>(pageDto.getNow(), pageDto.getSize());
        //条件构造器
        QueryWrapper queryWrapper = new QueryWrapper();
        if (pageDto.getWhere().getTitle() != null) {
            queryWrapper.like("title", pageDto.getWhere().getTitle());
        }
        IPage pageInfo = this.page(page, queryWrapper);

        PageResult<Repo> pageResult = new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords(), pageInfo.getPages());

        return pageResult;
    }

    @Override
    @Transactional
    public ApiRest saveRepo(Repo repo) {
        //参数校验
        if (repo == null) {
            throw new ServiceException("参数异常");
        }
        if (repo.getTitle() == null || repo.getTitle() == "") {
            throw new ServiceException("参数异常");
        }
        //前端传参为题库名称、题库描述，剩余信息在后端补全
        repo.setCreateTime(new Date());
        repo.setUpdateTime(new Date());

        /**
         * mybatis-plus提供的saveOrUpdate方法
         *      若是实体类中对应数据库主键的属性有值，则调用update更新
         *      若是实体类中对应数据库主键的属性没有值，则调用save增加
         */
        boolean save = this.saveOrUpdate(repo);
        return save ? ApiRest.success() : ApiRest.failure();
    }

    @Override
    @Transactional
    public ApiRest deleteRepo(String repoId) {
        boolean delete = this.removeById(repoId);
        return delete ? ApiRest.success() : ApiRest.failure();
    }

    @Override
    @Transactional
    public ApiRest deleteIds(String[] ids) {
        //数据校验
        if (ids.length == 0) {
            throw new ServiceException("至少选择一项");
        }
        boolean deleteIds = this.removeBatchByIds(Arrays.asList(ids));
        return deleteIds ? ApiRest.success() : ApiRest.failure();
    }
}
